package com.wyx.springbootspringkafkademo.controller;

import com.wyx.springbootspringkafkademo.entity.Message;
import com.wyx.springbootspringkafkademo.entity.User;
import com.wyx.springbootspringkafkademo.producer.KafkaProducerService;
import com.wyx.springbootspringkafkademo.producer.UserProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wyx
 * @Date 2025-03-26
 */
@RestController
@RequestMapping("/producer")
public class ProducerController {

  @Autowired
  private KafkaProducerService kafkaProducerService;
  @Autowired
  private UserProducer userProducer;

  @RequestMapping("/send")
  public String send(@RequestBody Message message) {
    kafkaProducerService.send(message);
    return "success";
  }

  @RequestMapping("/sendBatch")
  public String sendBatch() {
    kafkaProducerService.sendBatch();
    return "success";
  }
  @RequestMapping("/sendUser")
  public String sendUser() {
    User user = User.newBuilder()
        .setId(1)
        .setName("Alice")
        .build();
    userProducer.sendUser(user);
    return "success";
  }
  @RequestMapping("/sendBatchUser")
  public String sendBatchUser() {
    for (int i = 0; i <100 ; i++) {
      User user = User.newBuilder()
          .setId(i)
          .setName("Alice"+i)
          .setEmail("alice"+i+"@example.com")
          .build();
      userProducer.sendUser(user);
    }
    return "success";
  }
}
